package com.hiwan.report.web;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.framework.dao.plugin.Page;
import org.framework.dao.plugin.PageContext;
import org.framework.session.RequestContex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hiwan.platform.utils.StringUtils;
import com.hiwan.report.pojo.FrSdrEntry;
import com.hiwan.report.service.VideoShowService;
import com.hiwan.system.common.base.BaseController;
import com.hiwan.system.pojo.DeptInfo;
import com.hiwan.system.pojo.HiwanFile;
import com.hiwan.system.pojo.ParameterInfo;
import com.hiwan.system.pojo.UserInfo;
import com.hiwan.system.util.UserCacheUtils;

@Controller
@RequestMapping("/videoShow")
public class VideoShowAction extends BaseController{
	
	@Autowired
	VideoShowService videoShowService;
	private String path;
	
	@Value("${com.hiwan.file.path}")
	String basePath;
	
	@RequestMapping("/toQuery.do")
	public String toQuery(ModelMap model,HttpSession session) {
		return "videoShow/query";
	}
	@RequestMapping("/toPlay.do")
	public String toPlay(String path,ModelMap model) {
		this.path=path;
		model.put("path", path); 
		return "videoShow/play";
	}
	
	@RequestMapping("/list")
	public @ResponseBody Map list( FrSdrEntry bean, ModelMap model) {
	 
		
		List<FrSdrEntry> list= videoShowService.findListPage(bean);
		 
		
		
		Page page = PageContext.getPage();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", list);
 		map.put("pageCount", page.getTotalCount());   
		return map;
		 
	
		 
	}
	@RequestMapping("/getFile")
	public ResponseEntity<byte[]>  getFile(String path) throws IOException{ 
		String fileFullName = basePath + File.separator + path;
		System.err.println(fileFullName);
		HiwanFile file = new HiwanFile(new File(fileFullName));
		HttpHeaders headers = new HttpHeaders();  
	    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
	    headers.setContentDispositionFormData("attachment", URLEncoder.encode(path.substring(path.lastIndexOf("/")), "UTF-8"));
	    return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file.getFile()),headers,HttpStatus.OK); 
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
		
		 


}
